package com.my.oschina.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.my.oschina.R;
import com.my.oschina.app.AppContext;
import com.my.oschina.bean.User;
import com.my.oschina.common.AppFragmentPagerAdapter;
import com.my.oschina.common.MyViewPager;
import com.my.oschina.fragment.ActiveFragment;
import com.my.oschina.fragment.InfoBlogFragment;
import com.my.oschina.fragment.UserInfoFragment;

/**
 * 用户专页，管理了UserInfoFragment、ActiveFragment、InfoBlogFragment等3个Fragment
 */
public class UserCenterActivity extends OscBaseActivity {

	private MyViewPager mViewPager = null;
	private int _uid;
	private int _hisuid;
	private String _hisname;
	private String _username;
	private User mUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpager_layout);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setLogo(R.drawable.slm_action_user);
		getSupportActionBar().setTitle(R.string.user_center);

		// 获取新闻发布者、评论的评论者等的信息
		_hisuid = getIntent().getIntExtra("his_id", 0);
		_hisname = getIntent().getStringExtra("his_name");
		_username = getIntent().getStringExtra("his_name");
		_uid = ((AppContext) getApplication()).getLoginUid();

		mViewPager = (MyViewPager) findViewById(R.id.viewpager);
		UserFriendPagerAdapter ufAdapter = new UserFriendPagerAdapter(
				getSupportFragmentManager());
		mViewPager.setOffscreenPageLimit(3);
		mViewPager.setAdapter(ufAdapter);
		mViewPager.setOnPageChangeListener(onPageChangeListener);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.info))
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.active))
				.setTabListener(tabListener));
		actionBar.addTab(actionBar.newTab().setText(getString(R.string.blog))
				.setTabListener(tabListener));

	}

	/**
	 * TabListener
	 */
	ActionBar.TabListener tabListener = new ActionBar.TabListener() {

		@Override
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			if (mViewPager.getCurrentItem() != tab.getPosition()) {
				mViewPager.setCurrentItem(tab.getPosition());
			}
		}

		@Override
		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

		}

		@Override
		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

		}
	};

	ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			getSupportActionBar().setSelectedNavigationItem(position);
		}
	};

	/**
	 * 他的资料
	 */
	private Fragment getUserInfoFragment() {
		return getSupportFragmentManager().findFragmentByTag(
				UserInfoFragment.class.getName());
	}

	/**
	 * 他的动态
	 */
	private Fragment getActiveFragment() {
		return getSupportFragmentManager().findFragmentByTag(
				ActiveFragment.class.getName());
	}

	/**
	 * 他的博客
	 */
	private Fragment getBlogFragment() {
		return getSupportFragmentManager().findFragmentByTag(
				InfoBlogFragment.class.getName());
	}

	/**
	 * 继承了AppFragmentPagerAdapter，后者又继承了PagerAdapter
	 */
	class UserFriendPagerAdapter extends AppFragmentPagerAdapter {

		List<Fragment> list = new ArrayList<Fragment>();

		public UserFriendPagerAdapter(FragmentManager fm) {
			super(fm);
			// 用户信息
			if (getUserInfoFragment() == null) {
				list.add(UserInfoFragment.newInstance(_uid, _hisuid, _hisname));
			} else {
				list.add(getUserInfoFragment());
			}
			// 动态
			if (getActiveFragment() == null) {
				list.add(ActiveFragment.newInstance(_uid, _hisuid, _hisname));
			} else {
				list.add(getActiveFragment());
			}
			// 博客
			if (getBlogFragment() == null) {
				list.add(InfoBlogFragment.newInstance(_hisuid, _hisname));
			} else {
				list.add(getBlogFragment());
			}
		}

		@Override
		protected String getTag(int position) {
			List<String> tagList = new ArrayList<String>();
			tagList.add(UserInfoFragment.class.getName());
			tagList.add(ActiveFragment.class.getName());
			tagList.add(InfoBlogFragment.class.getName());
			return tagList.get(position);
		}

		@Override
		public Fragment getItem(int i) {
			return list.get(i);
		}

		@Override
		public int getCount() {
			return list.size();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_user_center, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			UserCenterActivity.this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
